package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*==================================
      时 间:   2023-03-20
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
===================================*/
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {

    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private HospitalService hospitalService;

    @PostMapping("/hospital/show")
    public Result<Hospital> getHospitalByHoscode(String hoscode,String sign){
        //1.进行signkey校验
        //2.
       Hospital hospital= hospitalService.getHospitalByHoscode(hoscode);
       return Result.ok(hospital);
    }

    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, String> paramMap= HttpRequestHelper.switchMap(parameterMap);
        //1.校验signKey:
        String hoscode = paramMap.get("hoscode");
        String platformSignKey=hospitalSetService.getSignKeyByHoscode(hoscode);
        String signKey = paramMap.get("sign");

        if(!StringUtils.isEmpty(signKey)&&signKey.equals(platformSignKey)){
            //2.保存医院信息
            String logoData = paramMap.get("logoData");
            paramMap.put("logoData", logoData.replaceAll(" ", "+"));
            hospitalService.save( paramMap);
        }
        return Result.ok();

    }
}
